home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / auth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-13  |  5.4 KB  |  232 lines

  1. /*
  2.  * authorization hooks for the server
  3.  *
  4.  * $XConsortium: auth.c,v 1.8 89/12/13 14:42:27 keith Exp $
  5.  *
  6.  * Copyright 1988 Massachusetts Institute of Technology
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of M.I.T. not be used in advertising or
  13.  * publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  M.I.T. makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * Author:  Keith Packard, MIT X Consortium
  19.  */
  20.  
  21. # include   "X.h"
  22. # include   "Xauth.h"
  23. # include   "misc.h"
  24.  
  25. struct protocol {
  26.     unsigned short   name_length;
  27.     char    *name;
  28.     int     (*Add)();        /* new authorization data */
  29.     XID        (*Check)();        /* verify client authorization data */
  30.     int     (*Reset)();        /* delete all authorization data entries */
  31.     XID        (*ToID)();        /* convert cookie to ID */
  32.     int        (*FromID)();    /* convert ID to cookie */
  33.     int        (*Remove)();    /* remove a specific cookie */
  34. };
  35.  
  36. extern int  MitAddCookie ();
  37. extern XID  MitCheckCookie ();
  38. extern int  MitResetCookie ();
  39. extern XID  MitToID ();
  40. extern int  MitFromID (), MitRemoveCookie ();
  41.  
  42. #ifdef HASDES
  43. extern int  XdmAddCookie ();
  44. extern XID  XdmCheckCookie ();
  45. extern int  XdmResetCookie ();
  46. extern XID  XdmToID ();
  47. extern int  XdmFromID (), XdmRemoveCookie ();
  48. #endif
  49.  
  50. static struct protocol   protocols[] = {
  51. {   (unsigned short) 18,    "MIT-MAGIC-COOKIE-1",
  52.         MitAddCookie,    MitCheckCookie,    MitResetCookie,
  53.         MitToID,    MitFromID,    MitRemoveCookie,
  54. },
  55. #ifdef HASDES
  56. {   (unsigned short) 19,    "XDM-AUTHORIZATION-1",
  57.         XdmAddCookie,    XdmCheckCookie,    XdmResetCookie,
  58.         XdmToID,    XdmFromID,    XdmRemoveCookie,
  59. },
  60. #endif
  61. };
  62.  
  63. # define NUM_AUTHORIZATION  (sizeof (protocols) /\
  64.                  sizeof (struct protocol))
  65.  
  66. /*
  67.  * Initialize all classes of authorization by reading the
  68.  * specified authorization file
  69.  */
  70.  
  71. static char *authorization_file = (char *)NULL;
  72.  
  73. static int  AuthorizationIndex = 0;
  74. static Bool ShouldLoadAuth = TRUE;
  75.  
  76. InitAuthorization (file_name)
  77. char    *file_name;
  78. {
  79.     authorization_file = file_name;
  80. }
  81.  
  82. int
  83. LoadAuthorization ()
  84. {
  85.     FILE    *f;
  86.     Xauth   *auth;
  87.     int        i;
  88.     int        count = 0;
  89.  
  90.     ShouldLoadAuth = FALSE;
  91.     if (!authorization_file)
  92.     return 0;
  93.     f = fopen (authorization_file, "r");
  94.     if (!f)
  95.     return 0;
  96.     AuthorizationIndex = 0;
  97.     while (auth = XauReadAuth (f)) {
  98.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  99.         if (protocols[i].name_length == auth->name_length &&
  100.         bcmp (protocols[i].name, auth->name, (int) auth->name_length) == 0)
  101.         {
  102.         ++count;
  103.         (*protocols[i].Add) (auth->data_length, auth->data,
  104.                      ++AuthorizationIndex);
  105.         }
  106.     }
  107.     XauDisposeAuth (auth);
  108.     }
  109.     fclose (f);
  110.     return count;
  111. }
  112.  
  113. #ifdef XDMCP
  114. /*
  115.  * XdmcpInit calls this function to discover all authorization
  116.  * schemes supported by the display
  117.  */
  118. RegisterAuthorizations ()
  119. {
  120.     int        i;
  121.  
  122.     for (i = 0; i < NUM_AUTHORIZATION; i++)
  123.     XdmcpRegisterAuthorization (protocols[i].name,
  124.                     (int)protocols[i].name_length);
  125. }
  126. #endif
  127.  
  128. XID
  129. CheckAuthorization (name_length, name, data_length, data)
  130. unsigned short    name_length;
  131. char    *name;
  132. unsigned short    data_length;
  133. char    *data;
  134. {
  135.     int    i;
  136.  
  137.     if (ShouldLoadAuth)
  138.     {
  139.     if (!LoadAuthorization())
  140.         EnableLocalHost ();
  141.     }
  142.     if (name_length)
  143.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  144.         if (protocols[i].name_length == name_length &&
  145.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  146.         {
  147.         return (*protocols[i].Check) (data_length, data);
  148.         }
  149.     }
  150.     return (XID) ~0L;
  151. }
  152.  
  153. ResetAuthorization ()
  154. {
  155.     int    i;
  156.  
  157.     for (i = 0; i < NUM_AUTHORIZATION; i++)
  158.     (*protocols[i].Reset)();
  159.     ShouldLoadAuth = TRUE;
  160. }
  161.  
  162. XID
  163. AuthorizationToID (name_length, name, data_length, data)
  164. {
  165.     int    i;
  166.  
  167.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  168.         if (protocols[i].name_length == name_length &&
  169.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  170.         {
  171.         return (*protocols[i].ToID) (data_length, data);
  172.         }
  173.     }
  174.     return (XID) ~0L;
  175. }
  176.  
  177. AuthorizationFromID (id, name_lenp, namep, data_lenp, datap)
  178. XID id;
  179. unsigned short    *name_lenp;
  180. char    **namep;
  181. unsigned short    *data_lenp;
  182. char    **datap;
  183. {
  184.     int    i;
  185.  
  186.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  187.     if ((*protocols[i].FromID) (id, data_lenp, datap)) {
  188.         *name_lenp = protocols[i].name_length;
  189.         *namep = protocols[i].name;
  190.         return 1;
  191.     }
  192.     }
  193.     return 0;
  194. }
  195.  
  196. RemoveAuthorization (name_length, name, data_length, data)
  197. unsigned short    name_length;
  198. char    *name;
  199. unsigned short    data_length;
  200. char    *data;
  201. {
  202.     int    i;
  203.  
  204.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  205.         if (protocols[i].name_length == name_length &&
  206.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  207.         {
  208.         return (*protocols[i].Remove) (data_length, data);
  209.         }
  210.     }
  211.     return 0;
  212. }
  213.  
  214. AddAuthorization (name_length, name, data_length, data)
  215. unsigned short    name_length;
  216. char    *name;
  217. unsigned short    data_length;
  218. char    *data;
  219. {
  220.     int    i;
  221.  
  222.     for (i = 0; i < NUM_AUTHORIZATION; i++) {
  223.         if (protocols[i].name_length == name_length &&
  224.         bcmp (protocols[i].name, name, (int) name_length) == 0)
  225.         {
  226.         return (*protocols[i].Add) (data_length, data,
  227.                     ++AuthorizationIndex);
  228.         }
  229.     }
  230.     return 0;
  231. }
  232.